package com.example.controller;

import com.example.common.BaseResponse;
import com.example.common.ErrorCode;
import com.example.common.ResultUtils;
import com.example.exception.BusinessException;
import com.example.model.PageBean;
import com.example.model.order.Order;
import com.example.model.order.OrderAddRequest;
import com.example.model.order.OrderAllInfo;
import com.example.model.order.OrderUpdateRequest;
import com.example.model.user.User;
import com.example.service.OrderService;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpRequest;
import org.springframework.web.bind.annotation.*;

import java.util.List;

import static com.example.contant.UserConstant.*;

@Slf4j
@RestController
@RequestMapping("/order")
public class OrderController {
    @Resource
    private OrderService orderService;


    /**
     * 新增订单
     * @param orderAddRequest 新增订单实体
     * @return 订单id
     */
    @PostMapping
    public BaseResponse<Long> addOrder(@RequestBody OrderAddRequest orderAddRequest) {
//        orderService.addOrder(roomAllInfo);
        if (orderAddRequest==null)
            throw new BusinessException(ErrorCode.NULL_ERROR);
        Long id=orderService.addOrder(orderAddRequest);
        if (id==null)
            throw new BusinessException(ErrorCode.SYSTEM_ERROR,"订单生成失败");
        log.info(id.toString());
        return ResultUtils.success(id);
    }

    //根据订单id获取订单信息
    @GetMapping
    public BaseResponse<OrderAllInfo> getOrderById(Long id) {
        if (id==null)
            throw new BusinessException(ErrorCode.NULL_ERROR);
        OrderAllInfo orderInfo=orderService.getOrderById(id);
        return ResultUtils.success(orderInfo);
    }

    //根据用户id获取订单信息
    @GetMapping("/userId")
    public BaseResponse<List<OrderAllInfo>> getByUserId(Long userId) {
        if (userId==null)
            throw new BusinessException(ErrorCode.NULL_ERROR);
        List<OrderAllInfo> orderList=orderService.getByUserId(userId);
//        log.info(ResultUtils.success(orderList).toString());
        return ResultUtils.success(orderList);
    }

    //获取所有订单信息
    @GetMapping("/all")
//    public BaseResponse<List<OrderAllInfo>> getAllOrder(HttpServletRequest request) {
    public BaseResponse<PageBean> getAllOrder(@RequestParam(defaultValue = "1") Integer pageNum,
                                              @RequestParam(defaultValue = "15")Integer pageSize,
                                              String orderNumber,
                                              Long hotelId,
                                              String username,
                                              HttpServletRequest request) {
        if (!isAdmin(request)){
            throw new BusinessException(ErrorCode.NO_AUTH);
        }
        PageBean result=orderService.getAllOrder(pageSize,pageNum,orderNumber,hotelId,username);
        return ResultUtils.success(result);
    }


    //根据酒店获取订单信息
    @GetMapping("/hotelId")
    public  BaseResponse<List<OrderAllInfo>> getByHotelId(Long hotelId,HttpServletRequest request){
        if (!isAdmin(request)){
            throw new BusinessException(ErrorCode.NO_AUTH);
        }
        List<OrderAllInfo> orderList=orderService.getByHotelId(hotelId);
        return ResultUtils.success(orderList);
    }

    //修改订单状态
    @PostMapping("/setStatus")
    public BaseResponse<Boolean> updateOrderStatus(@RequestBody OrderUpdateRequest orderUpdateRequest) {
        if (orderUpdateRequest==null){
            throw new BusinessException(ErrorCode.NULL_ERROR);
        }
        Long roomId= orderUpdateRequest.getRoomId();
        Long id=orderUpdateRequest.getId();
        Integer status=orderUpdateRequest.getOrderStatus();
        Boolean result=orderService.updateOrderStatus(id,status,roomId);
        return ResultUtils.success(result);
    }

    //是否为管理员
    private boolean isAdmin(HttpServletRequest request) {
        Object userObj = request.getSession().getAttribute(USER_LOGIN_STATE);
        User user = (User) userObj;
//        if (user == null || (user.getUserRole() != ADMIN_ROLE && user.getUserRole() != SUPER_ADMIN_ROLE))
//            return false;
//        return true;
        if (userObj == null || !(userObj instanceof User)) {
            // userObj 是 null 或者不是 User 类型，返回 false
            return false;
        }
        // 检查用户角色是否为 1 或 2
        return user.getUserRole() == ADMIN_ROLE || user.getUserRole() == SUPER_ADMIN_ROLE;
    }
}
